package com.anket.person.dao.impl;
 
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.anket.address.model.Address;
import com.anket.city.model.City;
import com.anket.country.model.Country;
import com.anket.district.model.District;
import com.anket.option.model.Option;
import com.anket.person.dao.PersonDao;
import com.anket.person.model.Person;
import com.anket.poll.model.Poll;
import com.hibernate.util.HibernateUtil;
 
public class PersonDaoImpl implements PersonDao{
	
	public Address prepareAddress(Address address){
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		
		
		long country= address.getCountry().getCountryId();
		long city= address.getCity().getCityId();
		long district= address.getDistrict().getDistrictId();
		String shortAddress=address.getShortAddress();
		
		
		Address newAddress=new Address();
		
		
		newAddress.setShortAddress(shortAddress);
		Query q=session.createQuery("from Country where countryId=:id");
		q.setLong("id", country);
		
		newAddress.setCountry((Country) q.list().get(0));
		
		q=session.createQuery("from City where cityId=:id");
		q.setLong("id", city);
		
		newAddress.setCity((City) q.list().get(0));
		
		q=session.createQuery("from District where districtId=:id");
		q.setLong("id", district);
		
		newAddress.setDistrict((District) q.list().get(0));
		
		return newAddress;
	}
	
	
	public void addPerson(Person person) {
		person.setCreatedDate(new Date());
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		
//		Address address=person.getAddress();
//		long country= address.getCountry().getCountryId();
//		long city= address.getCity().getCityId();
//		long district= address.getDistrict().getDistrictId();
//		String shortAddress=address.getShortAddress();
//		
//		
//		Address newAddress=new Address();
//		
//		
//		newAddress.setShortAddress(shortAddress);
//		Query q=session.createQuery("from Country where countryId=:id");
//		q.setLong("id", country);
//		
//		newAddress.setCountry((Country) q.list().get(0));
//		
//		q=session.createQuery("from City where cityId=:id");
//		q.setLong("id", city);
//		
//		newAddress.setCity((City) q.list().get(0));
//		
//		q=session.createQuery("from District where districtId=:id");
//		q.setLong("id", district);
//		
//		newAddress.setDistrict((District) q.list().get(0));
//		
//		person.setAddress(newAddress);
		
		session.save(person);
	}
		

	
	public void deletePerson(Person person) {
		// TODO Auto-generated method stub
	}

	
	public void updatePerson(Person person) {
		// TODO Auto-generated method stub
	}


	
	public Person getPersonById(Integer id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Query q = session.createQuery("from Person where personId=:id");
		q.setLong("id", id.longValue());
		List list=q.list();
		return (Person) list.get(0);
	}

	
	public boolean isPersonAdmin(Person person) {
		return false;
	}

	
	public Person isloginTrue(String email, String password) {	
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Query q = session.createQuery("from Person where email=:email and password=:pass");
		q.setString("email", email);
		q.setString("pass", password);
		List list=q.list();
		return (list.size()!=0)?(Person) list.get(0):null;		
	}

	
	public void addPoll(Poll poll) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.save(poll);
	}

	
	
	public void addOption(Option option) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.save(option);	
	}


	

 

}